/**
 *
 * 
 *
 */

export function BQGIS_video() {
    this.domList = {}
    this.hls = {}
}

BQGIS_video.prototype.setFlvVideo = function (videoElement, flvUrl) {
    if (window.flvjs.isSupported()) {
        const flvPlayer = window.flvjs.createPlayer({
            type: "flv",
            url: flvUrl
        })
        flvPlayer.attachMediaElement(videoElement)
        flvPlayer.load()
        flvPlayer.play()
    } else {
        console.log("不支持flv格式视频")
    }
    return videoElement
}

BQGIS_video.prototype.setHLSVideo = function (videoElement, hlsUrl) {
    if (window.Hls.isSupported()) {
        const hls = new window.Hls()
        hls.attachMedia(videoElement)
        hls.on(Hls.Events.MEDIA_ATTACHED, function () {
            hls.loadSource(hlsUrl)
            hls.on(window.Hls.Events.MANIFEST_PARSED, function () {
                videoElement.play()
            })
        })
        return hls
    } else if (videoElement.canPlayType("application/vnd.apple.mpegurl")) {
        videoElement.src = hlsUrl
        videoElement.addEventListener("loadedmetadata", function () {
            videoElement.play()
        })
        return
    }
}

BQGIS_video.prototype.createVideoElement = function (id){
    if(!id) return
    if(id in this.domList){
        // return
        return this.domList[id].dom
    }
    const videoElement = mars3d.DomUtil.create("video", "", document.body)
    if(id){
        videoElement.setAttribute("id", id)
    }
    videoElement.setAttribute("muted", "muted")
    videoElement.setAttribute("autoplay", "autoplay")
    videoElement.setAttribute("loop", "loop")
    videoElement.setAttribute("crossorigin", "")
    videoElement.setAttribute("controls", "")
    videoElement.style.display = "none"
    this.domList[id] =  {
        dom: videoElement,
        state: 'stop'
    }
    return videoElement
}

BQGIS_video.prototype.setDomState = function (id ,type) {
    if(id in this.domList) {
        this.domList[id].type = type
        this.domList[id].state = 'play'
    }
}

BQGIS_video.prototype.destroy = function (){
    for(let id in this.domList){
        this.removeDom(id)
    }
    for(let id in this.hls) {
        this.removeHis(id)
    }
}

BQGIS_video.prototype.removeVideoElement = function (id) {
    this.removeDom(id)
    this.removeHis(id)
}

BQGIS_video.prototype.removeDom = function (id) {
    if(id in this.domList) {
        document.body.removeChild(this.domList[id].dom)
        this.domList[id].dom.remove()
        this.domList[id].dom = null
        delete this.domList[id]
    }
}

BQGIS_video.prototype.removeHis = function (id) {
    if(id in this.hls){
        this.hls[id].destroy()
        delete this.hls[id]
    }
}

BQGIS_video.prototype.setVideoSrc = function ({src, videoElement}) {
    const id = videoElement.id
    if(id in this.domList && this.domList[id].state === 'play') return // 已经播放，则不进行设置
    return this.setSrc({src, videoElement})
}

BQGIS_video.prototype.changeVideoSrc = function ({src, videoElement}) {
    const id = videoElement.id
    videoElement.pause()
    this.removeHis(id)
    return this.setSrc({src, videoElement})
}

BQGIS_video.prototype.setSrc = function ({src, videoElement}) {
    if (/\.(mp4)$/.test(src)) {
        videoElement.setAttribute("src", src)
        this.setDomState(videoElement.id, 'mp4')
    } else if (/\.(flv)$/.test(src)) {
        this.setFlvVideo(videoElement, src)
        this.setDomState(videoElement.id, 'flv')
    } else if (/\.(m3u8)$/.test(src)) {
        this.hls[videoElement.id] = this.setHLSVideo(videoElement, src)
        this.setDomState(videoElement.id, 'hls')
    }
    return videoElement
} 